<?php
defined('BASEPATH') or exit('No direct script access allowed');

class MysqlModel extends CI_Model
{

    protected $table;

    /* 构造 */
    public function __construct()
    {
        parent::__construct();
        $this->load->database();
    }

    /* 设置数据表 */
    public function setTable($table)
    {
        $this->table = $table;
    }

    /* 插入 */
    public function insert($value)
    {
        $table = $this->table;
        $result = $this->db->insert($table, $value);
        return $result;
    }

    /* 删除 */
    public function delete($condition)
    {
        $table = $this->table;
        $where = $this->db->where($condition);
        $result = $this->db->delete($table);
        return $result;
    }

    /* 查询 */
    public function find($condition = array(), $field = array('*'), $orderBy = 'id asc', $limit = array(), $like = array())
    {
        $table = $this->table;
        $fieldStr = implode(',', $field);
        $select = $this->db->select($fieldStr);
        $from = $this->db->from($table);
        $where = $this->db->where($condition);
        $order = $this->db->order_by($orderBy);
        if (!empty($limit)) {
            $limit = $this->db->limit($limit['number'], $limit['start']);
        }
        if (! empty($like)) {
            $like = $this->db->like($like);
        }
        $query = $this->db->get();
        $result = $query->result_array();
        return $result;
    }

    /* 修改 */
    public function update($conditon, $value)
    {
        $table = $this->table;
        $where = $this->db->where($conditon);
        $set = $this->db->set($value);
        $result = $this->db->update($table);
        return $result;
    }
    
    /* 统计数目  */
    public function count($condition = array())
    {
        $table = $this->table;
        $from = $this->db->from($table);
        $where = $this->db->where($condition);
        $count = $this->db->count_all_results();
        return $count;
    }
}

